package com.morristaedt.mirror.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import com.morristaedt.mirror.R;
import com.morristaedt.mirror.configuration.ConfigurationSettings;
import com.morristaedt.mirror.modules.voice.TtsModule;

public class SetUpActivity extends Activity {


    @NonNull
    private ConfigurationSettings mConfigSettings;


    private RadioGroup mTemperatureChoice;
    private CheckBox mVoiceCheckbox;
    private TextView tvVoiceSelect;
    private CheckBox mFaceCheckbox;
    private View mColorShowView;
    private SeekBar mColorPickerRed;
    private SeekBar mColorPickerGreen;
    private SeekBar mColorPickerBlue;
    private TextView mColorShowerRed;
    private TextView mColorShowerGreen;
    private TextView mColorShowerBlue;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_configuration);
        setTheme(R.style.BaseAppTheme);

        mConfigSettings = new ConfigurationSettings(this);

        mTemperatureChoice = (RadioGroup) findViewById(R.id.temperature_group);
        mTemperatureChoice.check(mConfigSettings.getIsCelsius() ? R.id.celsius : R.id.farenheit);

        mColorPickerRed = (SeekBar) findViewById(R.id.ColorPickerRed);
        mColorPickerRed.setProgress(Color.red(mConfigSettings.getTextColor()));

        mColorPickerGreen = (SeekBar) findViewById(R.id.ColorPickerGreen);
        mColorPickerGreen.setProgress(Color.green(mConfigSettings.getTextColor()));

        mColorPickerBlue = (SeekBar) findViewById(R.id.ColorPickerBlue);
        mColorPickerBlue.setProgress(Color.blue(mConfigSettings.getTextColor()));

        mColorShowerRed = (TextView) findViewById(R.id.ColorShowerRed);
        mColorShowerRed.setText(String.format("%d", Color.red(mConfigSettings.getTextColor())));

        mColorShowerGreen = (TextView) findViewById(R.id.ColorShowerGreen);
        mColorShowerGreen.setText(String.format("%d", Color.green(mConfigSettings.getTextColor())));

        mColorShowerBlue = (TextView) findViewById(R.id.ColorShowerBlue);
        mColorShowerBlue.setText(String.format("%d", Color.blue(mConfigSettings.getTextColor())));

        mColorShowView = findViewById(R.id.colored_bar);
        mColorShowView.setBackgroundColor(mConfigSettings.getTextColor());

        mColorPickerRed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mConfigSettings.setTextColorRed(progress);
                mColorShowerRed.setText(String.format("%d", progress));
                mColorShowView.setBackgroundColor(mConfigSettings.getTextColor());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        mColorPickerGreen.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mConfigSettings.setTextColorGreen(progress);
                mColorShowerGreen.setText(String.format("%d", progress));
                mColorShowView.setBackgroundColor(mConfigSettings.getTextColor());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });

        mColorPickerBlue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mConfigSettings.setTextColorBlue(progress);
                mColorShowerBlue.setText(String.format("%d", progress));
                mColorShowView.setBackgroundColor(mConfigSettings.getTextColor());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });


        mVoiceCheckbox = (CheckBox) findViewById(R.id.cb_voice_detection);
        tvVoiceSelect = (TextView) findViewById(R.id.tv_voice_select);
        mVoiceCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    tvVoiceSelect.setVisibility(View.VISIBLE);
                }else {
                    tvVoiceSelect.setVisibility(View.GONE);
                }
            }
        });
        mVoiceCheckbox.setChecked(mConfigSettings.showVoiceDetection());
        tvVoiceSelect.setText(TtsModule.cloudVoicersEntries[mConfigSettings.getTtsVoice()]);
        tvVoiceSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(SetUpActivity.this).setTitle("选择魔镜的声音")
                        .setItems(TtsModule.cloudVoicersEntries, // 单选框有几项,各是什么名字
                                new DialogInterface.OnClickListener() { // 点击单选框后的处理
                                    public void onClick(DialogInterface dialog,
                                                        int which) { // 点击了哪一项
                                        mConfigSettings.putTtsVoice(which);
                                        tvVoiceSelect.setText(TtsModule.cloudVoicersEntries[which]);
                                        dialog.dismiss();
                                    }
                                }).show();
            }
        });

        mFaceCheckbox = (CheckBox) findViewById(R.id.cb_face_detection);
        mFaceCheckbox.setChecked(mConfigSettings.showFaceDetection());
    }

    public void onFinished(View view){
        saveFields();

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                imm.hideSoftInputFromWindow(mStockTickerSymbol.getWindowToken(), 0);

        Intent intent = new Intent(SetUpActivity.this, MirrorActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void saveFields() {
        mConfigSettings.setIsCelsius(mTemperatureChoice.getCheckedRadioButtonId() == R.id.celsius);
        mConfigSettings.setShowVoiceDetection(mVoiceCheckbox.isChecked());
        mConfigSettings.setFaceDetection(mFaceCheckbox.isChecked());

    }
}
